package com.cpr.demo.controller;

import cn.hutool.core.lang.UUID;
import com.cpr.demo.mycraft.mq.redis.RedisMessage;
import com.cpr.demo.mycraft.mq.redis.RedisMessageQueueManager;
import com.cpr.demo.service.PersonCache2ServiceImpl;
import com.cpr.demo.service.PersonCacheServiceImpl;
import com.cpr.demo.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Chen Panrui
 * created at 2021/10/9 15:29
 */
@RestController
@RequestMapping("/mq")
@Slf4j
public class MqTestController {

    @Autowired
    @Qualifier("singleRedisMQ")
    private RedisMessageQueueManager singleRedisMq;
    @Autowired
    @Qualifier("multiRedisMQ")
    private RedisMessageQueueManager multiRedisMq;

    @Qualifier("personService1")
    @Autowired
    private PersonService personService1;
    @Qualifier("personService2")
    @Autowired
    private PersonService personService2;


    /**
     * 发布消息 - 只会被消费一次
     * 订阅的代码demo {@link PersonCacheServiceImpl#afterPropertiesSet()}
     * @param queue 队列名
     * @param message 消息
     * @return 结果
     */
    @GetMapping("/single/send")
    public String send1(@RequestParam("queue") String queue, @RequestParam("message") String message){
        singleRedisMq.publish(queue, RedisMessage.buildMessage(UUID.randomUUID().toString(true), message));
        return "success";
    }

    /**
     * 发布消息 - 被多次消费
     * 订阅的代码demo {@link PersonCache2ServiceImpl#afterPropertiesSet()}
     * @param queue 队列名
     * @param message 消息
     * @return 结果
     */
    @GetMapping("/multi/send")
    public String send2(@RequestParam("queue") String queue, @RequestParam("message") String message){
        multiRedisMq.publish(queue, RedisMessage.buildMessage(UUID.randomUUID().toString(true), message));
        return "success";
    }

    @GetMapping("/person1/get")
    public String getPerson1(@RequestParam("whom") String whom){
        return personService1.getInfo(whom);

    }@GetMapping("/person2/get")
    public String getPerson2(@RequestParam("whom") String whom){
        return personService2.getInfo(whom);
    }

}
